logo头像

危锦辉的博客

Java 时间工具类

1、说明

收藏自己使用到的各种时间操作的工具类,方便以后自己使用,只做记录。

2、code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package top.zywork.common.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtils {
public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
public static final String MINUTE_PATTERN = "yyyy-MM-dd HH:mm";
public static final String HOUR_PATTERN = "yyyy-MM-dd HH:mm:ss";
public static final String DATE_PATTERN = "yyyy-MM-dd";
public static final String MONTH_PATTERN = "yyyy-MM";
public static final String YEAR_PATTERN = "yyyy";
public static final String MINUTE_ONLY_PATTERN = "mm";
public static final String HOUR_ONLY_PATTERN = "HH";

/**
* 日期相加减天数
* @param date 如果为Null,则为当前时间
* @param days 加减天数
* @param includeTime 是否包括时分秒,true表示包含
* @return
* @throws ParseException
*/
public static Date dateAdd(Date date, int days, boolean includeTime) throws ParseException{
if(date == null){
date = new Date();
}
if(!includeTime){
SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.DATE_PATTERN);
date = sdf.parse(sdf.format(date));
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, days);
return cal.getTime();
}

/**
* 时间格式化成字符串
* @param date Date 需要格式化的时间
* @param pattern 指定格式化后的格式,如果为空,则为yyyy-MM-dd
* @return
* @throws ParseException
*/
public static String dateFormat(Date date, String pattern) throws ParseException{
if(StrUtils.isBlank(pattern)){
pattern = DateUtils.DATE_PATTERN;
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(date);
}

/**
* 字符串解析成时间对象
* @param dateTimeString String类型的时间字符串
* @param pattern 字符串的时间格式,如果为空,则为yyyy-MM-dd
* @return
* @throws ParseException
*/
public static Date dateParse(String dateTimeString, String pattern) throws ParseException{
if(StrUtils.isBlank(pattern)){
pattern = DateUtils.DATE_PATTERN;
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.parse(dateTimeString);
}

/**
* 将日期时间格式成只有日期的字符串(可以直接使用dateFormat,Pattern为Null进行格式化)
* @param dateTime Date 需要格式话的时间日期
* @return
* @throws ParseException
*/
public static String dateTimeToDateString(Date dateTime) throws ParseException{
String dateTimeString = dateFormat(dateTime, DATE_TIME_PATTERN);
return dateTimeString.substring(0, 10);
}

/**
* 当时、分、秒为00:00:00时,将日期时间格式成只有日期的字符串,
* 当时、分、秒不为00:00:00时,直接返回
* @param dateTime Date
* @return
* @throws ParseException
*/
public static String dateTimeToDateStringIfTimeEndZero(Date dateTime) throws ParseException{
String dateTimeString = DateUtils.dateFormat(dateTime, DateUtils.DATE_TIME_PATTERN);
if(dateTimeString.endsWith("00:00:00")){
return dateTimeString.substring(0, 10);
}else{
return dateTimeString;
}
}

/**
* 将日期时间格式成日期对象,和dateParse互用
* @param dateTime Date
* @return Date
* @throws ParseException
*/
public static Date dateTimeToDate(Date dateTime) throws ParseException{
Calendar cal = Calendar.getInstance();
cal.setTime(dateTime);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}

/**
* 时间加减小时
* @param startDate 要处理的时间,Null则为当前时间
* @param hours 加减的小时
* @return Date
*/
public static Date dateAddHours(Date startDate, int hours) {
if (startDate == null) {
startDate = new Date();
}
Calendar c = Calendar.getInstance();
c.setTime(startDate);
c.set(Calendar.HOUR, c.get(Calendar.HOUR) + hours);
return c.getTime();
}

/**
* 时间加减分钟
* @param startDate 要处理的时间,Null则为当前时间
* @param minutes 加减的分钟
* @return
*/
public static Date dateAddMinutes(Date startDate, int minutes) {
if (startDate == null) {
startDate = new Date();
}
Calendar c = Calendar.getInstance();
c.setTime(startDate);
c.set(Calendar.MINUTE, c.get(Calendar.MINUTE) + minutes);
return c.getTime();
}

/**
* 时间加减秒数
* @param startDate 要处理的时间,Null则为当前时间
* @param minutes 加减的秒数
* @return
*/
public static Date dateAddSeconds(Date startDate, int seconds) {
if (startDate == null) {
startDate = new Date();
}
Calendar c = Calendar.getInstance();
c.setTime(startDate);
c.set(Calendar.SECOND, c.get(Calendar.SECOND) + seconds);
return c.getTime();
}

/**
* 时间加减天数
* @param startDate 要处理的时间,Null则为当前时间
* @param days 加减的天数
* @return Date
*/
public static Date dateAddDays(Date startDate, int days) {
if (startDate == null) {
startDate = new Date();
}
Calendar c = Calendar.getInstance();
c.setTime(startDate);
c.set(Calendar.DATE, c.get(Calendar.DATE) + days);
return c.getTime();
}

/**
* 时间加减月数
* @param startDate 要处理的时间,Null则为当前时间
* @param months 加减的月数
* @return Date
*/
public static Date dateAddMonths(Date startDate, int months) {
if (startDate == null) {
startDate = new Date();
}
Calendar c = Calendar.getInstance();
c.setTime(startDate);
c.set(Calendar.MONTH, c.get(Calendar.MONTH) + months);
return c.getTime();
}

/**
* 时间加减年数
* @param startDate 要处理的时间,Null则为当前时间
* @param years 加减的年数
* @return Date
*/
public static Date dateAddYears(Date startDate, int years) {
if (startDate == null) {
startDate = new Date();
}
Calendar c = Calendar.getInstance();
c.setTime(startDate);
c.set(Calendar.YEAR, c.get(Calendar.YEAR) + years);
return c.getTime();
}

/**
* 时间比较(如果myDate>compareDate返回1,<返回-1,相等返回0)
* @param myDate 时间
* @param compareDate 要比较的时间
* @return int
*/
public static int dateCompare(Date myDate, Date compareDate) {
Calendar myCal = Calendar.getInstance();
Calendar compareCal = Calendar.getInstance();
myCal.setTime(myDate);
compareCal.setTime(compareDate);
return myCal.compareTo(compareCal);
}

/**
* 获取两个时间中最小的一个时间
* @param date
* @param compareDate
* @return
*/
public static Date dateMin(Date date, Date compareDate) {
if(date == null){
return compareDate;
}
if(compareDate == null){
return date;
}
if(1 == dateCompare(date, compareDate)){
return compareDate;
}else if(-1 == dateCompare(date, compareDate)){
return date;
}
return date;
}

/**
* 获取两个时间中最大的一个时间
* @param date
* @param compareDate
* @return
*/
public static Date dateMax(Date date, Date compareDate) {
if(date == null){
return compareDate;
}
if(compareDate == null){
return date;
}
if(1 == dateCompare(date, compareDate)){
return date;
}else if(-1 == dateCompare(date, compareDate)){
return compareDate;
}
return date;
}

/**
* 获取两个日期(不含时分秒)相差的天数,不包含今天
* @param startDate
* @param endDate
* @return
* @throws ParseException
*/
public static int dateBetween(Date startDate, Date endDate) throws ParseException {
Date dateStart = dateParse(dateFormat(startDate, DATE_PATTERN), DATE_PATTERN);
Date dateEnd = dateParse(dateFormat(endDate, DATE_PATTERN), DATE_PATTERN);
return (int) ((dateEnd.getTime() - dateStart.getTime())/1000/60/60/24);
}

/**
* 获取两个日期(不含时分秒)相差的天数,包含今天
* @param startDate
* @param endDate
* @return
* @throws ParseException
*/
public static int dateBetweenIncludeToday(Date startDate, Date endDate) throws ParseException {
return dateBetween(startDate, endDate) + 1;
}

/**
* 获取两个日期相差的小时
* @param startDate
* @param endDate
* @return
* @throws ParseException
*/
public static int dateBetweenHour(Date startDate, Date endDate) throws ParseException {
return (int) ((end.getTime() - start.getTime()) / (60 * 60 * 1000));
}

/**
* 获取两个日期相差的分钟
* @param startDate
* @param endDate
* @return
* @throws ParseException
*/
public static int dateBetweenHour(Date startDate, Date endDate) throws ParseException {
return (int) ((end.getTime() - start.getTime()) / (60 * 1000));
}

/**
* 获取日期时间的年份,如2017-02-13,返回2017
* @param date
* @return
*/
public static int getYear(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.YEAR);
}

/**
* 获取日期时间的月份,如2017年2月13日,返回2
* @param date
* @return
*/
public static int getMonth(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.MONTH) + 1;
}

/**
* 获取日期时间的月份,如2017年2月13日,返回2
* @param date
* @return
*/
public static int getMonth(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.MONTH) + 1;
}

/**
* 获取日期时间的星期(即返回星期几),如2017-02-13,返回星期三
* @param date
* @return
*/
public static int getDate(Date date) {
String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int dayIndex = calendar.get(calendar.DAY_OF_WEEK) - calendar.SUNDAY;
if (dayIndex < 0) {
dayIndex = 0;
}
return weekDays[dayIndex];
}

/**
* 获取日期时间当月的总天数,如2017-02-13,返回28
* @param date
* @return
*/
public static int getDaysOfMonth(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.getActualMaximum(Calendar.DATE);
}

/**
* 获取日期时间当年的总天数,如2017-02-13,返回2017年的总天数
* @param date
* @return
*/
public static int getDaysOfYear(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.getActualMaximum(Calendar.DAY_OF_YEAR);
}

/**
* 根据时间获取当月最大的日期
* <li>2017-02-13,返回2017-02-28</li>
* <li>2016-02-13,返回2016-02-29</li>
* <li>2016-01-11,返回2016-01-31</li>
* @param date Date
* @return
* @throws Exception
*/
public static Date maxDateOfMonth(Date date) throws Exception {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int value = cal.getActualMaximum(Calendar.DATE);
return dateParse(dateFormat(date, MONTH_PATTERN) + "-" + value, null);
}

/**
* 根据时间获取当月最小的日期,也就是返回当月的1号日期对象
* @param date Date
* @return
* @throws Exception
*/
public static Date minDateOfMonth(Date date) throws Exception {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int value = cal.getActualMinimum(Calendar.DATE);
return dateParse(dateFormat(date, MONTH_PATTERN) + "-" + value, null);
}
}